package com.horizon.mobile.activity;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.base.BaseHttpListActivity;
import zuo.biao.library.interfaces.AdapterCallBack;
import zuo.biao.library.interfaces.OnBottomDragListener;
import zuo.biao.library.manager.HttpManager.OnHttpResponseListener;
import zuo.biao.library.util.JSON;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.horizon.mobile.R;
import com.horizon.mobile.adapter.TodoAdapter;
import com.horizon.mobile.manage.SharePreferences;
import com.horizon.mobile.model.Result;
import com.horizon.mobile.model.Todo;
import com.horizon.mobile.util.HttpRequest;

/**
 * 已发起列表
 */
public class GetHasMethodListActivity extends
BaseHttpListActivity<Todo, TodoAdapter> implements
		OnBottomDragListener {
	public int page = 1;

	public static final String TITLE = "TITLE";
	public static final String METHOD = "METHOD";
	ImageView clear_ico, search_ico, search_ico_right;
	EditText drawer_search_input;
	public String searchString = "";
	public List<Todo> listTodo = new ArrayList<Todo>();
	public static final String RESULT_CLICKED_ITEM = "RESULT_CLICKED_ITEM";

	/**
	 * 启动这个Activity的Intent
	 * 
	 * @param context
	 * @return
	 */
	public static Intent createIntent(Context context, String title,String method) {
		return new Intent(context, GetHasMethodListActivity.class).putExtra(
				TITLE, title).putExtra(METHOD, method);
	}

	// 启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	@Override
	public Activity getActivity() {
		return this;
	}

	private String  title = "";
	private String method ="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO demo_list_activity改为你所需要的layout文件；传this是为了底部左右滑动手势
		setContentView(R.layout.todo_list_activity, this);

		intent = getIntent();
		title = intent.getStringExtra(TITLE);
		method= intent.getStringExtra(METHOD);
		// initCache(this);//初始化缓存，Entry<String, String>替换成不带类型的类才可使用，原因看
		// .CacheCallBack

		// 功能归类分区方法，必须调用<<<<<<<<<<
		initView();
		initData();
		initEvent();
		// 功能归类分区方法，必须调用>>>>>>>>>>

		onRefresh();
	}

	// UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

	@Override
	public void initView() {// 必须在onCreate方法内调用
		super.initView();
		drawer_search_input = (EditText) findViewById(R.id.drawer_search_input);
		// 设置该SearchView默认是否自动缩小为图标
		clear_ico = (ImageView) findViewById(R.id.clear_ico);
		search_ico = (ImageView) findViewById(R.id.search_ico);
		search_ico_right = (ImageView) findViewById(R.id.search_ico_right);
	}


	@Override
	public void getListAsync(int page) {
//		showProgressDialog(R.string.loading);
		this.page = page;
		HttpRequest.getMethodList(method,SharePreferences.getInstance(context).getToken(),searchString, page, 10, 0, listener);
	}

	// UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	// Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

	@Override
	public void initData() {// 必须在onCreate方法内调用
		super.initData();
		// 示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

		tvBaseTitle.setText(title);


		// 示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	}

	@Override
	public void onRefresh() {
		searchString = "";
		drawer_search_input.setText("");
		super.onRefresh();
	}

	@Override
	public void initEvent() {// 必须在onCreate方法内调用
		super.initEvent();
		// 示例代码<<<<<<<<<<<<<<<<<<<
		drawer_search_input.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable edit) {
				if (drawer_search_input.getText().toString().length() > 0) {
					clear_ico.setVisibility(View.VISIBLE);
					search_ico.setVisibility(View.GONE);
					search_ico_right.setVisibility(View.VISIBLE);
				} else {
					clear_ico.setVisibility(View.GONE);
					search_ico.setVisibility(View.VISIBLE);
					search_ico_right.setVisibility(View.GONE);
				}

			}
		});
		clear_ico.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				drawer_search_input.setText("");
				searchString="";
				getListAsync(1);
			}
		});
		drawer_search_input
				.setOnEditorActionListener(new OnEditorActionListener() {

					@Override
					public boolean onEditorAction(TextView textView,
							int actionId, KeyEvent keyEvent) {
						if (actionId == EditorInfo.IME_ACTION_SEARCH) {
							searchString = drawer_search_input.getText()
									.toString();
							getListAsync(1);
						}
						return true;
					}
				});
		search_ico_right.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				searchString = drawer_search_input.getText()
						.toString();
				getListAsync(1);
			}
		});
		lvBaseList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(listTodo.get(position-1).getWorkId().length()==0){
					toActivity(FlowDetailActivity.createIntent(context,
							listTodo.get(position-1).getId(), listTodo.get(position-1)
									.getTitle(),listTodo.get(position-1).getTrackId(),""));
				}else{
					toActivity(FlowDetailActivity.createIntent(context,
							listTodo.get(position-1).getWorkId(), listTodo.get(position-1)
									.getTitle(),listTodo.get(position-1).getTrackId(),""));
				}
				
			}
		});
		// 示例代码>>>>>>>>>>>>>>>>>>>
	}

	@Override
	public void onDragBottom(boolean rightToLeft) {
		if (rightToLeft) {

			return;
		}

		finish();
	}

	@Override
	public void setList(final List<Todo> list) {
		listTodo = list;
		setList(new AdapterCallBack<TodoAdapter>() {

			@Override
			public void refreshAdapter() {
				adapter.refresh(list);

			}

			@Override
			public TodoAdapter createAdapter() {
				// TODO Auto-generated method stub
				return new TodoAdapter(context);
			}
		});

	}

	OnHttpResponseListener listener = new OnHttpResponseListener() {

		@Override
		public void onHttpResponse(int requestCode, String resultJson,
				Exception e) {
			boolean isNull = resultJson == null;
			if (resultJson != null) {
				Result result = JSON.parseObject(resultJson, Result.class);
				if (result.isSuccess()) {
					List<Todo> list = JSON.parseArray(result.getData()
							.toString(), Todo.class);
					onLoadSucceed(page, list);
					// setList(list);
				} else {
					showShortToast(result.getMessage());
				}
			} else {
				if (e != null)
					showShortToast(e.getMessage());
				else
					showShortToast("连接服务器失败！");
			}
		}

	};
	@Override
	public List<Todo> parseArray(String json) {
		return JSON.parseArray(json, Todo.class);
	}
}